package com.example.yyy

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_function.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.startActivityForResult
import java.lang.reflect.GenericArrayType
import java.text.SimpleDateFormat
import java.util.*

class FunctionActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_function)
        getDinnerStr();
        getDinnerStr("ss","sdf","dfs","eee")
        getDinnerStr(egg = "eee")
        appendString<Int>("www",1,2,3,4)
        appendString<String>("www","1","2","3","4")
        setArrayStr<Int>(arrayOf(1,2,3,4))
        //setArrayStr<String>("www","1","2","3","4")
        var str :Array<String> = arrayOf("how","do","you")
        maxCustom(str,{a,c->a.length>c.length})
        maxCustom(str,{a,c->a>c})
        var nowData:String = Date().getNowData();
        var nowDate:String = DateUtil.nowDate
        btn_btn.setOnClickListener {
            startActivityForResult<ImageTextActivity>(0)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
    }
    //void 省略 没输入参数，也没输出参数
    fun getDinnerEmpty(){

    }
    //只有输入参数 不可null
    fun getDinnerInput(egg:String){

    }
    //只有输入参数 可null
    fun getDinnerCanNull(egg: String?){

    }
    //返回值为void
    fun getDinnerVoid():Unit{

    }
    //返回值为String
    fun getDinnerString(egg:String):String{
        return "";
    }
    //默认参数
    fun getDinnerStr(egg:String="wwww"):String{
        return egg;
    }
    //参数个数不确定
    fun getDinnerStr(egg:String="wwww",vararg other:String?):String{
        return egg;
    }
    //泛型函数
    fun <T> appendString (tag : String , vararg otherInfo :T?):String{
        var str = tag;
        for(item in otherInfo){
            str = "$str${item.toString()}";
        }
        return str;
    }
    //内联函数
    inline fun <reified T : Number> setArrayStr(array:Array<T>) {

    }
    //简化函数 尾递归函 函数尾部递归调用自身，那么可加上关键字tailrec表示这是一个尾递归函数
    // 此时编译器会自动优化递归，即用循环方式代替递归，从而避免栈溢出的情况
    tailrec fun fac(n:Int):Int{
        return if (n<=1) n else n*fac(n-1);
    }
    //高阶函数
    //允许将函数表达式作为输入参数传进来，就形成了高阶函数，这里的greater函数就像是个变量
    //greater函数有两个输入参数，返回布尔型的输出参数
    //如果第一个参数大于第二 个参数，就认为greater返回true, 否则返回false
    fun <T> maxCustom(array: Array<T>,greater:(T,T) ->Boolean):T?{
        var max:T? = null;
        for (item in array){
            if (max==null||greater(item,max))
                max = item
        }
        return max;
    }
    //扩展函数
    fun Date.getNowData():String{
        val sdf = SimpleDateFormat("yyyy-MM-dd")
        return sdf.format(this)
    }
}